﻿using System;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;
using UnityEngine;
namespace AutoChess
{
    public enum HeroEffectType
    {
        None,
        /// <summary> 战场中有2个相同的英雄 </summary>
        Field2Hero = 2,
        /// <summary> 战场中有三个相同的棋子 </summary>
        Field3Chess =3
    }
    /// <summary>
    /// 逻辑实体
    /// </summary>
    /*public class HeroUnitEx:BaseUnit
    {
        public int ChessId;
        public int heroId;
        /// <summary>所属的玩家战场 </summary>
        public AutoChessFieldCtr GameAutoChessField;
        /// <summary> 所在格子 </summary>
        public ACGrid mapGrid;
        /// <summary> 关联的渲染器 </summary>
        public HeroUnitRenderer heroUnitRenderer;
        /// <summary> 所属玩家 </summary>
        public PlayerEntity playerEntity;

        public Vector3 Position;
        private HeroEffectType heroEffectType = HeroEffectType.None;
        public bool isShowUpStar = false;

        public bool isDeathHide = false; //是否死亡隐藏

        private Table.DataModel.TableAutoChessHero heroTable;
        private Table.DataModel.TableAutoChessHeroChess chessTable;
        public Table.DataModel.TableAutoChessHeroChess ChessTable
        {
            get { return chessTable; }
        }
        public Table.DataModel.TableAutoChessHero HeroTable
        {
            get { return heroTable; }
        }
        public HeroEffectType HeroEffectType
        {
            get { return heroEffectType; }
        }

        public void SetShowStar(bool showStar)
        {
            this.isShowUpStar = showStar;
        }
        public void SetFieldHeroEffectType(HeroEffectType type )
        {
            heroEffectType = type;
        }

        public override void OnBorn()
        {
            chessTable = Table.Blo.AutoChessBlo.GetChessTable(ChessId);
            if (chessTable != null)
               heroId = chessTable.heroId;
            heroTable = Table.Blo.AutoChessBlo.GetChessHero(heroId);
        }

        public override void OnDestroy()
        {
            heroTable = null;
            chessTable = null;
        }

        public override void ConnectRenderer(BaseRenderer renderer)
        {
            var temp = renderer as HeroUnitRenderer;
            if (renderer == null)
            {
                Debug.LogError("HeroUnit connect renderer failed. RD is null");
                return;
            }
            DisconnectRenderer();

            heroUnitRenderer = temp;
            heroUnitRenderer.OnConnect(this);
        }

        /// <summary>
        /// Buff 棋子状态变更
        /// </summary>
        /// <param name="hero"></param>
        public void OnUpStarBuff(AutoChessChessInfo hero)
        {
            ChessId = hero.chessId;
            if (heroUnitRenderer != null)
            {
                heroUnitRenderer.OnUpStarBuff();
              
            }
        }

        public void OffUpStarBuff(AutoChessChessInfo hero)
        {
            ChessId = hero.chessId;
            if (heroUnitRenderer != null)
            {
                heroUnitRenderer.OnOffStarBuff();
            }
        }

        public override void DisconnectRenderer()
        {
            if (heroUnitRenderer != null)
            {
                heroUnitRenderer.OnDisconnect();
                heroUnitRenderer = null;
            }
        }

        #region OnEnter OnLeave
        /// <summary>
        /// 进入战场
        /// </summary>
        public void EnterGameModeField(AutoChessFieldCtr modeAutoChessField,GridUnit bornGrid)
        {
            //TODO 添加进入战场事件
            if (modeAutoChessField != null && bornGrid != null)
            {
                this.GameAutoChessField = modeAutoChessField;
                //进入格子
                EnterGrid(bornGrid);
            }
        }

        /// <summary>
        /// 离开战场 (升级、出售的时候) ，离开格子断开
        /// </summary>
        public void LeaveBattleField()
        {
            if (heroUnitRenderer != null)
            {
                heroUnitRenderer.StopAllCoroutines();
                heroUnitRenderer.OnDisconnect();
            }
            LeaveGrid();
            playerEntity.RemoveHeroUnit(this);
        }

        /// <summary>
        /// 进入格子
        /// </summary>
        public void EnterGrid(GridUnit grid)
        {
            if (grid == null)
            {
                Debug.LogError(string.Format("HeroUnit {0} enter grid failed , grid is null.", ChessId ));
                return;
            }
            //看下当前在的格子上的英雄是不是 自己， 不是自己说明已经有别人进入了。 这个时候自己要进入这个新格子。 但是别人进入的不能把他清除
            //如果自己还在当前格子，自己退出来 然后进入新的格子
            var fromGrid = mapGrid;
            if (mapGrid != null && mapGrid.heroUnit == this)
            {
                LeaveGrid();
            }
            //通知格子被进入了
            grid.OnEnter(this);
            mapGrid = grid;
            UpdatePositionRenderer(fromGrid, mapGrid);
        }

        private void UpdatePositionRenderer(GridUnit fromGrid, GridUnit toGrid)
        {
            if (heroUnitRenderer != null)
            {
                heroUnitRenderer.UpdatePosition(fromGrid, toGrid);
            }
        }

        /// <summary>
        /// 离开当前格子
        /// </summary>
        private void LeaveGrid()
        {
            if (mapGrid != null)
            {
                mapGrid.OnLeave();
                mapGrid = null;
            }
        }
        #endregion

        #region FightView

        //复活
        public void Relife()
        {
            isDeathHide = false;
            if (heroUnitRenderer != null)
            {
                heroUnitRenderer.SetModelActiveState(!isDeathHide);
            }
        }
        //死亡
        public void HeroDeath()
        {
            if (heroUnitRenderer != null)
            {
                heroUnitRenderer.SetModelActiveState(!isDeathHide);
            }
        }

        public void JumpToPosition(Vector3 targetPos, JumpType jumptype, System.Action jumpEnd)
        {
            if (heroUnitRenderer != null)
            {
                heroUnitRenderer.JumpToTarget(targetPos, jumptype , jumpEnd);
            }
        }

        public void ShowUp(System.Action end)
        {
            if (heroUnitRenderer != null)
            {
                heroUnitRenderer.ShowUp(end);
            }
        }

        public void Show(string animName, Action end)
        {
            if(heroUnitRenderer != null)
            {
                heroUnitRenderer.Show(animName, end);
            }
        }
        #endregion

        #region interface

        public void RefrehRenderer()
        {
            if (heroUnitRenderer != null)
            {
                heroUnitRenderer.RefreshRenderer();
            }
        }
        public override string ToString()
        {
            if (mapGrid != null)
            {
                return string.Format("HeroUnit_{0}_{1}_{2}", mapGrid.GridType.ToString(), ChessId, mapGrid.ServerIndex);
            }
            else
            {
                return string.Format("HeroUnit_{0}_{1}", ChessId, playerEntity.playerGuid);
            }
        }

  
        #endregion

    }*/
}
